#include "include/book.h"
// 解决byte命名冲突问题
#define byte win_byte_override
#include "include/mysql.h"
#undef byte

// 全局变量
extern MysqlUser* mysql;

Book::Book():tripId(0),clientId(0),bookingId(0),bookTime(0),refundTime(0) {}
Book::~Book(){}



Book Book::SearchBook(int64_t clientId) {
    std::stringstream sqlStream;
    sqlStream << "select * from booking where clientId = " << to_string(clientId);
    std::string sqlStr = sqlStream.str();

    vector<vector<string>> result = mysql->select(sqlStr);

    this->setBookingId(stoi(result[0][0]));
    this->setClientId(stoll (result[0][1]));
    this->setTripId(stoi(result[0][2]));
    this->setParseBookTime(ParseTimeStyle(stoll(result[0][3])));
    if (result[0][4] == "NULL") {
        this->setParseRefundTime("NULL");
    }
    else {
        this->setParseRefundTime(ParseTimeStyle(stoll(result[0][4])));
    }

    return *this;
}


vector<Book> Book::SearchAllBook() {
    vector<Book> books;
    string sqlStr = "select * from booking";
    vector<vector<string>> results = mysql->select(sqlStr);

    // 对结果进行装载
    vector<vector<string>>::iterator it = results.begin();
    for (it; it != results.end(); it++) {
        vector<string>::iterator it2 = (*it).begin();

        Book p;
        p.setBookingId(stoi(it2[0]));
        p.setClientId(stoll(it2[1]));
        p.setTripId(stoi(it2[2]));
        p.setParseBookTime(ParseTimeStyle(stoll(it2[3])));
        if (it2[4] == "NULL") {
            p.setParseRefundTime("NULL");
        }
        else {
            p.setParseRefundTime(ParseTimeStyle(stoll(it2[4])));
        }

        books.push_back(p);
    }
    return books;
}



unsigned int Book::getBookingId() const {
    return bookingId;
}

int64_t Book::getClientId() const {
    return clientId;
}

unsigned int Book::getTripId() const {
    return tripId;
}

long long Book::getBookTime() const {
    return bookTime;
}

long long Book::getRefundTime() const {
    return refundTime;
}

string Book::getParseBookTime() const {
    return ParseBookTime;
}

string Book::getParseRefundTime() const {
    return ParseRefundTime;
}

// 设置器方法
void Book::setBookingId(unsigned int id) {
    bookingId = id;
}

void Book::setClientId(int64_t id) {
    clientId = id;
}

void Book::setTripId(unsigned int id) {
    tripId = id;
}

void Book::setBookTime(long long time) {
    bookTime = time;
}

void Book::setRefundTime(long long time) {
    refundTime = time;
}


void Book::setParseBookTime(string time) {
    ParseBookTime = time;
}


void Book::setParseRefundTime(string time) {
    ParseRefundTime = time;
}